//
//  LXSignRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/6.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXSignRecordViewController.h"
#import "LXSignContractApplyViewController.h"
#import "LXSignContractModel.h"
#import "LXSignRecordListController.h"
@interface LXSignRecordViewController ()<UIScrollViewDelegate>
@property (nonatomic) UIView *topView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) NSString *searchStr;
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UIButton *topBtn;
@property (nonatomic) UIImageView *topImgV;
@property (nonatomic) NSInteger selIndex;
/**今日签约*/
@property (nonatomic) UILabel *todaySignLab;
/**全部签约*/
@property (nonatomic) UILabel *allSignLab;
@property (nonatomic) NSMutableArray *titlesAry;


@end
#define lineWidth 17
@implementation LXSignRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [kNotifyCenter postNotificationName:@"signListRefresh" object:nil];
    }

    [self setSignCount];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    [self layoutCustomNav:self.view title:@"签约记录"];
    
    [self layoutView];
    
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backBtn.frame = CGRectMake(kScreenWidth - 55, KStatuesBarHeight, 40, 40);
    [backBtn setImage:[UIImage imageNamed:@"add_sign"] forState:(UIControlStateNormal)];
    [self.view addSubview:backBtn];
    [backBtn whenTapped:^{
        LXSignContractApplyViewController *vc = [[LXSignContractApplyViewController alloc] init];
        vc.type = 1;
        [self pushToNextController:vc];
        
    }];
    [[kNotifyCenter rac_addObserverForName:@"setSignCount" object:nil] subscribeNext:^(NSNotification * _Nullable note) {
        
        [self setSignCount];
        
    }];
    
}

- (void)setSignCount{
    NSString *url = [DOMAIN_URL stringByAppendingFormat:SigncontractPage_URL];
    [MSNetworkManager getRevertUrl:url params:@{@"page":@(1),@"rows":@(10)} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            // 今日签约
            NSString *todaySignCount = [NSString stringWithFormat:@"%@",resonseObj[@"todaySign"]];
            self.todaySignLab.text = [NSString stringWithFormat:@"%@\n今日签约",todaySignCount];
            [self.todaySignLab addAttributes:TextAttrCorlor(mainColor) forText:todaySignCount size:[UIFont systemFontOfSize:26 weight:UIFontWeightSemibold]];

            // 全部
            NSString *allSignCount = [NSString stringWithFormat:@"%@",resonseObj[@"allSign"]];
            self.allSignLab.text = [NSString stringWithFormat:@"%@\n全部签约",allSignCount];
            [self.allSignLab addAttributes:TextAttrCorlor(mainColor) forText:allSignCount size:[UIFont systemFontOfSize:26 weight:UIFontWeightSemibold]];
            
            // 即将到期
            NSString *soonOverDueCount = [NSString stringWithFormat:@"%@",resonseObj[@"soonOverDue"]];
            // 逾期未处理
            NSString *waitProcessCount = [NSString stringWithFormat:@"%@",resonseObj[@"waitProcess"]];
            // 逾期未处理
            NSString *rescindCount = [NSString stringWithFormat:@"%@",resonseObj[@"rescind"]];
            
            NSArray *ary = @[@"全部",[NSString stringWithFormat:@"即将到期(%@)",soonOverDueCount],[NSString stringWithFormat:@"到期未处理(%@)",waitProcessCount],[NSString stringWithFormat:@"已解约(%@)",rescindCount]];
            
            
            for (int i = 0; i < self.titlesAry.count; i++) {
                UIButton *btn = self.titlesAry[i];
                [btn setTitle:ary[i] forState:UIControlStateNormal];
            }
        }
        
    
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:NO];
}



-(void)layoutView{
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, 180)];
    topView.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    self.topView = topView;
    [self.view addSubview:topView];
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 15, ScreenWidth/2 - 10, 70)];
    [view1 addCorner:3];
    view1.backgroundColor = UIColor.whiteColor;
    [topView addSubview:view1];
 
    
    UILabel *todaySignLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, view1.width, 50)];
    todaySignLab.textAlignment = NSTextAlignmentCenter;
    todaySignLab.numberOfLines = 2;
    todaySignLab.text = @"0\n今日签约";
    todaySignLab.lineSpace = 5;
    todaySignLab.font = [UIFont systemFontOfSize:14];
    todaySignLab.textColor = Color333;
    [view1 addSubview:todaySignLab];
    [todaySignLab addAttributes:TextAttrCorlor(mainColor) forText:@"0" size:[UIFont systemFontOfSize:26 weight:UIFontWeightSemibold]];
    self.todaySignLab = todaySignLab;
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(view1.X_width + 10, 15, ScreenWidth/2 - 20, 70)];
    [view2 addCorner:3];
    view2.backgroundColor = UIColor.whiteColor;
    [topView addSubview:view2];
    
    UILabel *allSignLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, view1.width, 50)];
    allSignLab.textAlignment = NSTextAlignmentCenter;
    allSignLab.numberOfLines = 2;
    allSignLab.text = @"0\n全部签约";
    allSignLab.lineSpace = 5;
    allSignLab.font = [UIFont systemFontOfSize:14];
    allSignLab.textColor = Color333;
    [view2 addSubview:allSignLab];
    [allSignLab addAttributes:TextAttrCorlor(mainColor) forText:@"0" size:[UIFont systemFontOfSize:26 weight:UIFontWeightSemibold]];
    self.allSignLab = allSignLab;
    
    LXCommSearchView *searchView = [LXCommSearchView shareInstance];
    searchView.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    searchView.frame = CGRectMake(0, view1.Y_height, kScreenWidth, 60);
    [searchView.searchBtn setTitle:@"请输入关键词搜索" forState:UIControlStateNormal];
    [topView addSubview:searchView];
    [searchView.searchBtn whenTapped:^{
    
        LXSignRecordListController *vc = [[LXSignRecordListController alloc] init];
        vc.isSearch = YES;
        [self pushToNextController:vc];
    }];
    
    NSArray *ary = @[@"全部",@"即将到期(0)",@"到期未处理(0)",@"已解约(0)"];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topView.Y_height, self.view.width, ScreenHeight - topView.Y_height)];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(ScreenWidth * ary.count, 0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    scrollView.delegate = self;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    
    
    UIScrollView *semScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, searchView.Y_height, ScreenWidth, 30)];
//    semScrollView.backgroundColor = [UIColor redColor];
    semScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [topView addSubview:semScrollView];
    
//    UIView *semView = [[UIView alloc] initWithFrame:CGRectMake(0, searchView.Y_height, ScreenWidth, 30)];
//    [topView addSubview:semView];
    self.titlesAry = [NSMutableArray array];
    CGFloat X = 15;
    for (int i = 0; i < ary.count; i++) {
        NSString *str = ary[i];
        CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:15 weight:UIFontWeightRegular] maxSize:CGSizeMake(100, 20)];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, size.width + 18, 20)];
        [btn setTitle:ary[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#AAAAAA"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.tag = i + 100;
//        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [semScrollView addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            self.selIndex = i;
            self.topBtn = btn;
            self.topBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
            self.topImgV = [[UIImageView alloc] initWithFrame:CGRectMake(X + (btn.width - lineWidth)/2, semScrollView.height-8, lineWidth, 6)];
            self.topImgV.image = kImg(@"分段下标");
            [semScrollView addSubview:self.topImgV];
        }
        else if(i == ary.count - 1){
            semScrollView.contentSize = CGSizeMake(btn.X_width + 10, 0);
        }
        [self.titlesAry addObject:btn];
    
        X = CGRectGetMaxX(btn.frame)+10;
        
        LXSignRecordListController *vc = [[LXSignRecordListController alloc] init];
        vc.view.frame = CGRectMake(i * ScreenWidth, 0, ScreenWidth, scrollView.height);
        vc.type = i;
        [scrollView addSubview:vc.view];
        [self addChildViewController:vc];
    }
    
 
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint p = scrollView.contentOffset;
    float w = p.x;
    int index = w  /scrollView.frame.size.width;
    
    self.topBtn.selected = NO;
    self.topBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];

    self.selIndex = index;
    UIButton *btn = [self.topView viewWithTag:index + 100];
    btn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];

    self.topImgV.X = btn.X + (btn.width - lineWidth)/2;
    btn.selected = YES;
    self.topBtn = btn;
    
}
#pragma mark -- 点击topView按钮
-(void)clickBtnAction:(UIButton *)sender{
    if (sender.isSelected) return;
    sender.selected = YES;
    sender.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    
    self.topBtn.selected = NO;
    self.topBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    self.topImgV.X = sender.X + (sender.width - lineWidth)/2;
    self.topBtn = sender;
    NSInteger pageCount = sender.tag - 100;
    self.selIndex = pageCount;
    [self.scrollView setContentOffset:CGPointMake(pageCount * self.scrollView.bounds.size.width, 0)];

}






@end
